﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.DataEntity;
using Cosmetic.Logic;
using System.Collections.Generic;

namespace WebApp.Management.Ad
{
    public partial class addxinwen : PageBase2
    {
        private articleclassManger articlelassmanger = new articleclassManger();
        private articleManger articleManger = new articleManger();
        private RoleManager m_RoleManager = new RoleManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.SignInUser1 == null)
            {
                Session.Remove("SignInUser1");
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请先登录权限!');window.top.location.href='/Management/indexs.aspx';", true);
                return;
            }
            if (!IsPostBack)
            {
                Cosmetic.DataEntity.User theUser = this.SignInUser1;
                if (!this.m_RoleManager.CheckUserRole(theUser, RoleManager.ERole.Admin))
                {
                    Session.Remove("SignInUser1");
                    this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('你没有管理员权限!');window.top.location.href='/Management/indexs.aspx';", true);
                    return;
                }
            }


            if (!IsPostBack)
            {

                //this.LoadFitCategory();
                //新增文章类别
                BindDropDownList(DropDownList1, this.articlelassmanger.GetAll());
                if (String.IsNullOrEmpty(this.Request.QueryString["PID"]))
                {
                    this.btnEditProduct.Visible = true;
                    this.Button1.Visible = false;
                }
                //修改文章类别
                else
                {
                    this.btnEditProduct.Visible = false;
                    this.Button1.Visible = true;
                    this.LoadProduct(Convert.ToInt32(this.Request.QueryString["PID"]));
                }
            }
        }


        private void LoadProduct(int aProductId)
        {
            article theProduct = this.articleManger.GetProductById(aProductId);
            this.fckBody.Value = theProduct.contents;
            this.TextBox1.Text = theProduct.tilte;
            //this.DropDownList1.SelectedValue = Convert .ToInt32(  theProduct.classid);
        }
        private void BindDropDownList(DropDownList aDropDownList, List<articleclass> aCategoryList)
        {
            aDropDownList.Items.Clear();
            if (aCategoryList.Count > 0)
            {
                aDropDownList.DataSource = aCategoryList;
                aDropDownList.DataTextField = "name";
                aDropDownList.DataValueField = "id";
                aDropDownList.DataBind();
            }

        }

        protected void btnEditProduct_Click(object sender, EventArgs e)
        {
            article numbers = new article();
            if (string.IsNullOrEmpty(this.fckBody.Value))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请填写内容!');", true);
                return;
            }
            numbers.contents = this.fckBody.Value;
            numbers.tilte = this.TextBox1.Text;
            numbers.classid = Convert.ToInt32(this.DropDownList1.SelectedValue.ToString());
            numbers.createTime = DateTime.Now;
            numbers.count = Convert.ToInt32(this.TextBox2.Text);
            this.articleManger.Insert(numbers);
            this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('添加成功!');", true);
            this.fckBody.Value = "";

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            article theProduct = this.articleManger.GetProductById(Convert.ToInt32(this.Request.QueryString["PID"]));

            if (string.IsNullOrEmpty(this.fckBody.Value))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请填写内容!');", true);
                return;
            }
            try
            {
                theProduct.contents = this.fckBody.Value;
                theProduct.classid = Convert.ToInt32(this.DropDownList1.SelectedValue.ToString());
                theProduct.tilte = this.TextBox1.Text;
                theProduct.createTime = DateTime.Now;
                theProduct.count = Convert.ToInt32(this.TextBox2.Text);
                this.articleManger.Update();
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('修改成功!');", true);
                this.Page.Response.Redirect("xinwenlists.aspx");
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('"+ex.Message+"!');", true);
            }
        }
    }
}
